// 坐标系及坐标转换http://epsg.io/?format=json&q=3857;
// 需要安装proj4 命令 cnpm install proj4 --save
import Projection from 'ol/proj/Projection'
import * as olProj from 'ol/proj'
import proj4 from 'proj4'

export default {
  data () {
    return {
      aRCS: {
        EPSG4490: {
          code: '4490',
          kind: 'CRS-GEOGCRS',
          bbox: [53.56, 73.62, 16.7, 134.77],
          wkt: 'GEOGCS["China Geodetic Coordinate System 2000",DATUM["China_2000",SPHEROID["CGCS2000",6378137,298.257222101,AUTHORITY["EPSG","1024"]],AUTHORITY["EPSG","1043"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4490"]]',
          unit: 'degree (supplier to define representation)',
          proj4: '+proj=longlat +ellps=GRS80 +no_defs',
          name: 'China Geodetic Coordinate System 2000',
          area: 'China - onshore and offshore.',
          default_trans: 0,
          trans: [],
          accuracy: ''
        },
        EPSG4326: {
          code: '4326',
          kind: 'CRS-GEOGCRS',
          bbox: [90.0, -180.0, -90.0, 180.0],
          wkt: 'GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]]',
          unit: 'degree (supplier to define representation)',
          proj4: '+proj=longlat +datum=WGS84 +no_defs',
          name: 'WGS 84',
          area: 'World.',
          default_trans: 0,
          trans: [],
          accuracy: ''
        },
        EPSG3857: {
          code: '3857',
          kind: 'CRS-PROJCRS',
          bbox: [85.06, -180.0, -85.06, 180.0],
          wkt: 'PROJCS["WGS 84 / Pseudo-Mercator",GEOGCS["WGS 84",DATUM["WGS_1984",SPHEROID["WGS 84",6378137,298.257223563,AUTHORITY["EPSG","7030"]],AUTHORITY["EPSG","6326"]],PRIMEM["Greenwich",0,AUTHORITY["EPSG","8901"]],UNIT["degree",0.0174532925199433,AUTHORITY["EPSG","9122"]],AUTHORITY["EPSG","4326"]],PROJECTION["Mercator_1SP"],PARAMETER["central_meridian",0],PARAMETER["scale_factor",1],PARAMETER["false_easting",0],PARAMETER["false_northing",0],UNIT["metre",1,AUTHORITY["EPSG","9001"]],AXIS["X",EAST],AXIS["Y",NORTH],EXTENSION["PROJ4","+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs"],AUTHORITY["EPSG","3857"]]',
          unit: 'metre',
          proj4: '+proj=merc +a=6378137 +b=6378137 +lat_ts=0.0 +lon_0=0.0 +x_0=0.0 +y_0=0 +k=1.0 +units=m +nadgrids=@null +wktext  +no_defs',
          name: 'WGS 84 / Pseudo-Mercator',
          area: 'World between 85.06\u00b0S and 85.06\u00b0N.',
          default_trans: 0,
          trans: [],
          accuracy: ''
        }
      }
    }
  },
  watch: {
    // watch
  },
  mounted () {
    // mounted
  },
  methods: {

    /**
     * @description 初始化Proj
     * @param sRCS {String}参考坐标系 名称
     * @return
     */
    // initProj (sRCS) {
    //   let proj4def = null
    //   let code = null
    //   let name = null
    //   let bbox = null
    //   let newProjCode = null
    //   switch(sRCS){
    //     case 'EPSG:4326':
    //       proj4def = aRCS[sRCS].proj4
    //       code = aRCS[sRCS].code
    //       name = aRCS[sRCS].name
    //       bbox = aRCS[sRCS].bbox
    //       break
    //     default:
    //   }
    //   // 定义注册新的proj
    //   newProjCode = 'EPSG:' + code
    //   proj4.defs(newProjCode, proj4def)
    //   register(proj4)

    // }

    /**
     * @description 定义EPSG:4490
     * @param sRCS {String}参考坐标系 名称
     * @return
     */
    addCRS4490 () {
      proj4.defs('EPSG:4490', '+proj=longlat+ellps=GRS80+no_defs')
      const proj4490 = new Projection({
        code: 'EPSG:4490',
        extent: [-179, -89.99, 179, 89.99],
        worldExtent: [-179, -89.99, 179, 89.99] // 不设置此项经纬度格出不来
      })
      olProj.addProjection(proj4490)
      olProj.addCoordinateTransforms('EPSG:4326', 'EPSG:4490',
        function (coordinate) {
          return proj4('EPSG:4326', 'EPSG:4490', coordinate)
        },
        function (coordinate) {
          return proj4('EPSG:4490', 'EPSG:4326', coordinate)
        }
      )
      olProj.addCoordinateTransforms('EPSG:3857', 'EPSG:4490',
        function (coordinate) {
          return proj4('EPSG:3857', 'EPSG:4490', coordinate)
        },
        function (coordinate) {
          return proj4('EPSG:4490', 'EPSG:3857', coordinate)
        }
      )
    }
    // 当前方法
  }
}
